package com.planshare.activity;

import java.net.MalformedURLException;
import java.net.URL;

import org.json.rpc.client.HttpJsonRpcClientTransport;
import org.json.rpc.client.JsonRpcInvoker;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.planshare.object.AndroidPlan;
import com.planshare.service.PlanService;
import com.planshare.util.UserUtil;

public class SearchPlanActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		ScrollView scrollView = new ScrollView(SearchPlanActivity.this);
		scrollView.setLayoutParams(new ScrollView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		
		final LinearLayout as = new LinearLayout(SearchPlanActivity.this);
		as.setOrientation(LinearLayout.VERTICAL);
		
		final LinearLayout searchLayout = new LinearLayout(SearchPlanActivity.this);
		searchLayout.setOrientation(LinearLayout.HORIZONTAL);
		searchLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		Button searchButton = new Button(SearchPlanActivity.this);
		searchButton.setText("Search");
		searchButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		final EditText searchEditText = new EditText(SearchPlanActivity.this);
		searchEditText.setHint("search plans");
		searchEditText.setLayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.0f));
		searchLayout.addView(searchEditText);
		searchLayout.addView(searchButton);
		searchButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				as.removeAllViews();
				as.addView(searchLayout);
				String searchString = searchEditText.getText().toString().toLowerCase();
						
						for (final AndroidPlan plan : UserUtil.allPlans) {
							if (plan.getName().toLowerCase().contains(searchString) || 
								plan.getLocation().toLowerCase().contains(searchString) || 
								plan.getDescription().toLowerCase().contains(searchString)) {
								TextView textView = new TextView(SearchPlanActivity.this);
								textView.setTextSize(16);
								textView.setText(plan.getName());
								textView.setOnClickListener(new OnClickListener() {

									@Override
									public void onClick(View v) {
										UserUtil.planViewed = plan;
										Intent intent = new Intent(SearchPlanActivity.this,
												ViewPlanActivity.class);
										startActivity(intent);
									}
								});
								
								as.addView(textView);
								
								TextView textViewLocation = new TextView(SearchPlanActivity.this);
								textViewLocation.setTextSize(10);
								textViewLocation.setText(plan.getLocation() + " - " + plan.getDate().toString());
								
								as.addView(textViewLocation);
								
								TextView gap = new TextView(SearchPlanActivity.this);
								gap.setTextSize(5);
								gap.setText("");
								
								as.addView(gap);
							}
						}
			}
		});
		
		as.addView(searchLayout);
		scrollView.addView(as);
		this.setContentView(scrollView);
		
		final ProgressDialog dialog = new ProgressDialog(
				SearchPlanActivity.this);
		dialog.setMessage("Retrieving data from server...");
		dialog.setIndeterminate(true);
		dialog.setCancelable(false);
		dialog.show();

		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub

				if (UserUtil.allPlans == null) {
					getPlans();
				}

				SearchPlanActivity.this.runOnUiThread(new Runnable() {
					public void run() {

						for (final AndroidPlan plan : UserUtil.allPlans) {
							// Create the text view
							TextView textView = new TextView(SearchPlanActivity.this);
							textView.setTextSize(16);
							textView.setText(plan.getName());
							textView.setOnClickListener(new OnClickListener() {

								@Override
								public void onClick(View v) {
									UserUtil.planViewed = plan;
									Intent intent = new Intent(SearchPlanActivity.this,
											ViewPlanActivity.class);
									startActivity(intent);
								}
							});

							as.addView(textView);
							
							TextView textViewLocation = new TextView(SearchPlanActivity.this);
							textViewLocation.setTextSize(10);
							textViewLocation.setText(plan.getLocation() + " - " + plan.getDate().toString());
							
							as.addView(textViewLocation);
							
							TextView gap = new TextView(SearchPlanActivity.this);
							gap.setTextSize(5);
							gap.setText("");
							
							as.addView(gap);
						}

						dialog.dismiss();
					}
				});
			}
		}).start();

	}

	private void getPlans() {
		HttpJsonRpcClientTransport transport;
		try {
			transport = new HttpJsonRpcClientTransport(new URL(
					UserUtil.planServiceUrl));
			JsonRpcInvoker invoker = new JsonRpcInvoker();
			PlanService calc = invoker
					.get(transport, "plan", PlanService.class);

			AndroidPlan[] plans = calc.getPlans(null, null);
			UserUtil.allPlans = plans;
			System.out.println(plans.length);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
